﻿using System;
using System.Drawing;

namespace Paint.NET_CRAW
{
    /// <summary>
    /// A color converter class
    /// </summary>
    public class Int24
    {
        private byte r;
        private byte g;
        private byte b;

        public byte R
        {
            get { return this.r; }
            set { this.r = value; }
        }
        public byte G
        {
            get { return this.g; }
            set { this.g = value; }
        }
        public byte B
        {
            get { return this.b; }
            set { this.b = value; }
        }

        public Int24(int num)
        {
            this.FromInt32(num);
        }

        public Int24(Color c)
        {
            this.FromColor(c);
        }

        public void FromInt32(int num)
        {
            int w = num;
            int temp;
            if (w < 0) w = Math.Abs(w);
            if (w > 0xFFFFFF) w = 0xFFFFFF;

            temp = w & 0xFF0000;
            temp = temp >> 16;
            this.r = Convert.ToByte(temp);

            temp = w & 0x00FF00;
            temp = temp >> 8;
            this.g = Convert.ToByte(temp);

            temp = w & 0x0000FF;
            this.b = Convert.ToByte(temp);
        }

        public void FromColor(Color c)
        {
            this.r = c.R;
            this.b = c.B;
            this.g = c.G;
        }

        public Int24()
        {
            this.r = 0;
            this.g = 0;
            this.b = 0;
        }

        public int ToInt32()
        {
            int temp = this.r << 16;
            int temp2 = this.g << 8;
            temp += temp2;
            temp2 = this.b;
            temp += temp2;
            return temp;
        }

        public byte[] ToByteArray()
        {
            byte[] ret = new byte[3];
            ret[0] = this.r;
            ret[1] = this.g;
            ret[2] = this.b;
            return ret;
        }

        public void FromByteArray(byte[] array)
        {
            byte backup;
            Array.Reverse(array);
            ushort b2, b;
            switch (array.Length)
            {
                case 1:
                    backup = (byte)(array[0] & 192);
                    this.r = (byte)(backup >> 5);
                    backup = (byte)(array[0] & 28);
                    this.g = (byte)(backup >> 2);
                    this.b = (byte)(array[0] & 3);
                    break;
                case 2:
                    //5650 format
                    b = BitConverter.ToUInt16(array, 0);
                    b2 = (ushort)(b & 0xf800);
                    this.r = (byte)(b2 >> 11);
                    b2 = (ushort)(b & 0x07e0);
                    this.g = (byte)(b2 >> 5);
                    this.b = (byte)(b & 0x001f);
                    break;
                case 3:
                    this.r = array[0];
                    this.g = array[1];
                    this.b = array[2];
                    break;
                case 4:
                    this.r = array[1];
                    this.g = array[2];
                    this.b = array[3];
                    break;
            }
        }

        public Color ToColor()
        {
            return Color.FromArgb(this.r, this.g, this.b);
        }

        public static int RGB2Int(byte R, byte G, byte B)
        {
            int temp = R << 16;
            int temp2 = G << 8;
            temp += temp2;
            temp2 = B;
            temp += temp2;
            return temp;
        }
    }
}
